//
// This script holds all the functions that record the movement of the scene entities
//

local PrevRecording = true

//
// Watch the entities' movement
//
local LSec = os.time()
local Frames = 0

function WatchEntities()
	if GA_Recording then
		//Check if this is the first time we're recording a new animation, if so, clean up the movement history in entities
		if GA_Recording != PrevRecording then ClearMovement() end
		
		//Add current position and angle to movement history
		for _, v in pairs( ents.FindByClass("prop_physics") ) do
			if v:GetNWBool("GAAnimated") and v.GAMovement then
				local MovementData = {}
				MovementData.Position = v:GetPos()
				MovementData.Angles = v:GetAngles()
				MovementData.Velocity = v:GetPhysicsObject():GetVelocity()
				MovementData.AngleVelocity = v:GetPhysicsObject():GetAngleVelocity()
				
				MovementData.Time = os.clock() - GA_StartTime
				
				table.insert( v.GAMovement, MovementData )
			end
		end
		
		if LSec != os.time() then
			Msg( "Recording with an fps of " .. Frames .. "\n" )
			
			Frames = 0
			LSec = os.time()
		else
			Frames = Frames + 1
		end
	end
	PrevRecording = GA_Recording
end
hook.Add( "Think", "WatchEntities", WatchEntities )

//
// Clear recorded movement
//
function ClearMovement()
	for _, v in pairs( ents.FindByClass("prop_physics") ) do
		if v:GetNWBool("GAAnimated") then v.GAMovement = {} end
	end
end